package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "ChangePasswordActivity";
    private Context mContext;
    private EditText changepass_oldpassword_et;
    private EditText changepass_newpassword_et;
    private EditText changepass_renewpassword_et;
    private Button changepass_ok_btn;
    private ImageView changepass_back_iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        mContext = this;

        initView();


    }

    private void initView() {
        //返回
        changepass_back_iv = findViewById(R.id.changepass_back_iv);
        changepass_back_iv.setOnClickListener(this);
        changepass_oldpassword_et = findViewById(R.id.changepass_oldpassword_et);
        changepass_newpassword_et = findViewById(R.id.changepass_newpassword_et);
        changepass_renewpassword_et = findViewById(R.id.changepass_renewpassword_et);
        changepass_ok_btn = findViewById(R.id.changepass_ok_btn);
        changepass_ok_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.changepass_back_iv://返回
                finish();
                break;
            case R.id.changepass_ok_btn://确认修改
                String oldpassword = changepass_oldpassword_et.getText().toString().trim();
                String newpassword = changepass_newpassword_et.getText().toString().trim();
                String renewpassword = changepass_renewpassword_et.getText().toString().trim();

                if (TextUtils.isEmpty(oldpassword) || TextUtils.isEmpty(newpassword) || TextUtils.isEmpty(renewpassword)){
                    Toast.makeText(mContext,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!newpassword.equals(renewpassword)){
                    Toast.makeText(mContext,"新密码不一致,请重新输入",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (oldpassword.length() < 4 || newpassword.length() < 4 || renewpassword.length() < 4 ){
                    Toast.makeText(mContext,"至少输入4位数密码",Toast.LENGTH_SHORT).show();
                    return;
                }

                JMessageClient.updateUserPassword(oldpassword, newpassword, new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        Log.i(TAG,"修改密码：返回编号:"+responseCode +" ,返回数据:"+responseMessage);

                        if (responseCode == 871201 && responseMessage.equals("Get response timeout,please try again later.")){
                            Toast.makeText(mContext,"响应超时，请稍后再试",Toast.LENGTH_SHORT).show();

                        }else if (responseCode == 898004 && responseMessage.equals("password error")){
                            Toast.makeText(mContext,"密码错误",Toast.LENGTH_SHORT).show();
                        }else if (responseCode == 0){
                            Toast.makeText(mContext,"修改成功",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(mContext, LoginActivity.class);
                            startActivity(intent);

                            finish();


                        }
                    }
                });


                break;
        }
    }
}
